package com.kgc.kmall.kmallcartservice.service;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.OmsCartItem;
import com.kgc.kmall.bean.OmsCartItemExample;
import com.kgc.kmall.kmallcartservice.mapper.OmsCartItemMapper;
import com.kgc.kmall.service.CartService;
import com.kgc.kmall.util.RedisUtil;
import io.searchbox.client.JestClient;
import org.apache.dubbo.config.annotation.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    OmsCartItemMapper omsCartItemMapper;

    @Resource
    RedisUtil redisUtil;

    @Override
    public OmsCartItem ifCartExistByUser(long memberId, long skuId) {
        OmsCartItemExample example=new OmsCartItemExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andProductSkuIdEqualTo(skuId);
        List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);
        return omsCartItems.size()>0?omsCartItems.get(0):null;
    }

    @Override
    public void addCart(OmsCartItem omsCartItem) {
       omsCartItemMapper.insert(omsCartItem);
    }

    @Override
    public void updateCart(OmsCartItem omsCartItem) {
       omsCartItemMapper.updateByPrimaryKeySelective(omsCartItem);
    }

    @Override
    public void flushCartCache(String memberId) {
        OmsCartItemExample example=new OmsCartItemExample();
        example.createCriteria().andMemberIdEqualTo(Long.parseLong(memberId));
        List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);
        Jedis jedis = redisUtil.getJedis();
        Map<String,String> map=new HashMap<>();
        for (OmsCartItem omsCartItem : omsCartItems) {
            map.put(omsCartItem.getProductSkuId().toString(), JSON.toJSONString(omsCartItem));
        }
        //删除原来jedis中有的
        jedis.del("user:"+memberId+":cart");
        //添加新的
        jedis.hmset("user:"+memberId+":cart",map);

    }

    @Override
    public List<OmsCartItem> cartList(String memberId) {
        Jedis jedis=null;
        List<OmsCartItem> omsCartItems=new ArrayList<>();
        try {
          jedis=redisUtil.getJedis();
            List<String> hvals = jedis.hvals("user:" + memberId + ":cart");
            for (String hval : hvals) {
                OmsCartItem omsCartItem = JSON.parseObject(hval, OmsCartItem.class);
                omsCartItems.add(omsCartItem);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
          jedis.close();
        }
        return omsCartItems;
    }

    @Override
    public void checkCart(OmsCartItem omsCartItem) {
        OmsCartItemExample example=new OmsCartItemExample();
        example.createCriteria().andMemberIdEqualTo(omsCartItem.getMemberId()).andProductSkuIdEqualTo(omsCartItem.getProductSkuId());
        omsCartItemMapper.updateByExampleSelective(omsCartItem,example);
        flushCartCache(omsCartItem.getMemberId().toString());
    }
}
